<?php 
include('../../Connections/KRAPPERCON.php');
$found = false;
include('../../Query_Min/getAlmacenes.php');
$combo_alm = $combo;
$usr = '';
$oper = getVal('oper');
		$usrprev = '';
		$feccre = '';
		$usrnom = '';
		$usrape = '';
		$usrnif = '';
		$usrcel = '';
		$usrtlf = '';
		$usrmail = '';
		$usrsts = '';
		$usrfin = '';
		$centro = '';
		$almacen = '';
		$direc = '';
		$fecnac = '';
		$mul = '';
?>

<?php 
//doEcho($oper);
if ($oper == 'view') {
$usr = getVal('usuario');
$sql = "call sp_getAllUsers('".$usr."');";
doEcho($sql);
$res = $mysqli->query($sql);
	if ($res){
		if ($row = $res->fetch_assoc()){
		$found = true;
		$usrprev = $row['UserPrev'];
		$feccre = $row['UserFeCr'];
		$usrnom = $row['UserNom'];
		$usrape = $row['UserApe'];
		$usrnif = $row['UserNIF'];
		$usrcel = $row['UserCel'];
		$usrtlf = $row['UserTelf'];
		$usrmail = $row['UserMail'];
		$usrsts = $row['UserStatus'];
		$usrfin = $row['UserFeIn'];
		$centro = $row['CentroID'];
		$almacen = $row['AlmacenID'];
		$direc = $row['UserDirec'];
		$fecnac = $row['UserFecN'];
		$multi = $row['UserMulti'];
		?>
        <script type="text/javascript">
			<?php 
			if ($found){
			?>
					GEBI('t_usr_usr').disabled = true;
					GEBI('t_usr_usr').value = '<?php  echo $usr;?>';
					GEBI('t_usr_nom').value = '<?php  echo $usrnom;?>';
					GEBI('t_usr_ape').value = '<?php  echo $usrape;?>';		
					GEBI('t_usr_nif').value = '<?php  echo $usrnif;?>';		
					GEBI('t_usr_cel').value = '<?php  echo $usrcel;?>';		
					GEBI('t_usr_tel').value = '<?php  echo $usrtlf;?>';		
					GEBI('t_usr_mail').value = '<?php  echo $usrmail;?>';		
					GEBI('t_usr_dir').value = '<?php  echo $direc;?>';	
					GEBI('t_usr_fin').value = '<?php  echo $usrfin;?>';			
					GEBI('t_usr_fna').value = '<?php  echo $fecnac;?>';
					GEBI('cbo_usr_sts').value = '<?php  echo $usrsts; ?>';
					GEBI('cbo_usr_cen').value = '<?php  echo $centro; ?>';
					GEBI('cbo_usr_alm').value = '<?php  echo $almacen; ?>';				
					GEBI('cbo_us_mul').value = '<?php echo $multi; ?>';	
					
			<?php 
			}
			?>
			</script>
        <?php
		}
		$res->close();
		$mysqli->check_result();
	}
}
elseif ($oper == 'upd') {
$usr = getVal('usr');
$nom = getVal('nom');
$ape = getVal('ape');
$dir = getVal('dir');
$nif = getVal('nif');
$fna = getVal('fna');
$cel = getVal('cel');
$mai = getVal('mai');
$sts = getVal('sts');
$cen = getVal('cen');
$alm = getVal('alm');
$fin = getVal('fin');
$pwd = getVal('pwd');
$mul = getVal('mul');

$sql = "call sp_updUsuario('".$usr."','".$ape."','".$nom."','".$nif."',
'".$dir."','".$fna."','".$cel."','".$cel."', '".$mai."','".$sts."',
'".$cen."', '".$alm."','".$fin."','".$user_now."','".$pwd."',".$mul.");";
$res = $mysqli->query($sql);
	if ($res){
		if ($row = $res->fetch_assoc()){
			if (trim($row['msg_err']) == ''){
				echo "<script type='text/javascript'>showOk('Se ha ".$row['msg_pro']." correctamente el usuario ".$usr."');</script>";				
			}
			else {
				echo "<script type='text/javascript'>showError('".$row['msg_err']."');</script>";
			}
		}
		$res->close();
		$mysqli->check_result();
	}
}
elseif ($oper == 'list'){
?>
<table width="100%" cellspacing="0" border="1" class="tblSort" rules="all" cellpadding="0">
<thead>
  <tr>
    <th>Usuario</th>
    <th>Nombres y Apellidos</th>
    <th>DNI</th>
    <th>Centro</th>
    <th>Almacen</th>
    <th>Status</th>
    <th>&nbsp;</th>
    </tr>
  </thead>
  <tbody>
  <?php 
  $sql = "call sp_getAllUsers('')";
  $res = $mysqli->query($sql);
  if ($res){
  	while($row = $res->fetch_assoc()){
	?>
    <tr>
    <td><a href="#" onClick="editUserSis(this)"><?php echo $row['UserID']; ?></a></td>
    <td><?php echo $row['UserNom']." ".$row['UserApe']; ?></td>
    <td><?php echo $row['UserNIF']; ?></td>
    <td><?php echo $row['CentroNom']; ?></td>
    <td><?php echo $row['AlmacenNom']; ?></td>
    <td><?php if($row['UserStatus']==0) echo "Inactivo"; else echo "Activo"; ?></td>
    <td>
      <a href="#" onclick="resetPWD('<?php echo $row['UserID'];?>');">Nueva Clave</a>
  </td>
  </tr>
    <?php 
	}
  $res->close();
  $mysqli->check_result();
  }
  ?>
  </tbody>
</table>
<?php 
}
elseif( $oper == 'aObj'){

$usr = getVal('user');
$obj = getVal('obj');

$sql = "call sp_addAccess('".$usr."','".$obj."',1);";
$res = $mysqli->query($sql) or die($mysqli->error.__LINE__);;

if($res){
	if ($row = $res->fetch_assoc()){
		$msg = $row['msg_err'];
		if (trim($msg)==''){
		$des = $row['des'];
		?>
        <script type="text/javascript">
		$('#tbl_aut_usr tbody').append("<tr title='<?php echo $obj;?>'><td><?php echo $des;?></td><td><input type='button' onclick='delAccUsr(this)' value='X'></td></tr>");
		$("#tbl_aut_usr").tablesorter({sortList: [[0,0]]});
		showOk('Se ha registrado el Acceso para <?php echo $usr;?>');</script>";
        <?php 
		}
		else {
		echo "<script type='text/javascript'>showError('".$msg."');</script>";
		}
	}
$res->close();
$mysqli->query($sql);
}

}
elseif( $oper == 'dObj'){

$usr = getVal('user');
$obj = getVal('obj');

$sql = "call sp_DelAccess('".$usr."','".$obj."');";
$res = $mysqli->query($sql) or die($mysqli->error.__LINE__);;

if($res){
	if ($row = $res->fetch_assoc()){
		$msg = $row['msg_err'];
		if (trim($msg)==''){
		echo "<script type='text/javascript'>$('#tbl_aut_usr tbody tr[title=".$obj."]').addClass('noView').detach().remove();".
			"showOk('Se ha removido el Acceso');</script>";
		}
		else {
		echo "<script type='text/javascript'>showError('".$msg."');</script>";
		}
	}
$res->close();
$mysqli->query($sql);
}

}
elseif ($oper == 'getObjs'){
$usr = getVal('user');
?>
<table id="tbl_aut_usr" width="100%" border="1" rules="all"  class="tableIn tblSort" cellspacing="0" cellpadding="0">
<caption>Autorizaciones del Usuario&nbsp;(<a href="#tbl_aut_usr" onclick="updAutUsr()">Actualizar</a>)
</caption>
<thead>
  <tr>
    <th>Descripcion</th>
    <th>&nbsp;</th>
    </tr>
  </thead>
  <tbody>
  <?php 
  $sql = "call sp_getObj_by_User('".$usr."');";
  $res = $mysqli->query($sql);
	if ($res){
	while($row = $res->fetch_assoc()){
	?>
  <tr title="<?php echo $row['ObjectID']; ?>">
    <td><?php echo $row['ObjectDesc']; ?></td>
    <td>
      <input type="button" onclick="delAccUsr(this)" value="X" />
</td>
    </tr>
    <?php 
	}
	$res->close();
	$mysqli->check_result();
	}
	?>
  </tbody>
</table>
<?php 
}
?>



